'use strict';

const {Controller} = require('egg');
const ResponseUtil = require('../utils/ResponseUtil');

class OrderController extends Controller {

    async getOrderList() {
        let query = this.ctx.query;
        query.afterSale = 0;
        const res = await this.ctx.service.order.getOrderList(query);
        return this.ctx.body = ResponseUtil.success(res.list, res.total);
    }

    async getOrderDetail(){
        let query = this.ctx.query;
        if(!query.orderId || !['0','1'].includes(query.afterSale)){
            return this.ctx.body = ResponseUtil.retArgErr();
        }
        query.afterSale = Number(query.afterSale);
        const res = await this.ctx.service.order.getOrderList(query);
        return this.ctx.body = ResponseUtil.success(res.list[0]);
    }

    async getOrderAfterSaleList() {
        let query = this.ctx.query;
        query.afterSale = 1;
        const res = await this.ctx.service.order.getOrderList(query);
        return this.ctx.body = ResponseUtil.success(res.list, res.total);
    }

    async orderConfirmRefund(){
        const res = await this.ctx.service.order.orderConfirmRefund();
        return this.ctx.body = ResponseUtil.success(res);
    }

    async orderRefusalRefund(){
        const res = await this.ctx.service.order.orderRefusalRefund();
        return this.ctx.body = ResponseUtil.success(res);
    }

    async getOrderCommentList() {
        let query = this.ctx.query;
        const res = await this.ctx.service.order.getOrderCommentList(query);
        return this.ctx.body = ResponseUtil.success(res.list, res.total);
    }

    async deleteOrderComment() {
        const res = await this.ctx.service.order.deleteOrderComment();
        return this.ctx.body = ResponseUtil.success(res);
    }

    async getOrderTicketList() {
        let query = this.ctx.query;
        const res = await this.ctx.service.order.getOrderTicketList(query);
        return this.ctx.body = ResponseUtil.success(res.list, res.total);
    }

    async orderConfirmDelivery() {
        const res = await this.ctx.service.order.orderConfirmDelivery();
        return this.ctx.body = ResponseUtil.success(res);
    }

    async getOrderChatList(){
        let query = this.ctx.query;
        const res = await this.ctx.service.order.getOrderChatList(query);
        return this.ctx.body = ResponseUtil.success(res.list, res.total);
    }

    async orderForceRefund(){
        const res = await this.ctx.service.order.orderForceRefund();
        return this.ctx.body = ResponseUtil.success(res);
    }

}

module.exports = OrderController;
